iT邦幫忙

2021 iThome 鐵人賽

DAY 20
0

大家好,我是毛毛。ヾ(´∀ ˋ)ノ
廢話不多說開始今天的解題Day~


242. Valid Anagram

Question

Given two strings s and t, return true if t is an anagram of s, and false otherwise.


Example

Example1

Input: s = "anagram", t = "nagaram"
Output: true

Example2

Input: s = "rat", t = "car"
Output: false

Constraints

  • 1 <= s.length, t.length <= 5 * 10^4
  • s and t consist of lowercase English letters.

Follow up:

What if the inputs contain Unicode characters? How would you adapt your solution to such a case?


解題

題目

首先先簡單的翻譯一下題目
給你兩個字串,要判斷這兩個字串是不是由相同種類跟數量的字母組成。

Think

作法大致上是這樣

  • Python的話,就用dictionary來存出現的字母與他對應的數量,最後再回傳判斷兩個dictionary是否相同的布林值。
  • C的話,做法跟Python很類似,也是建兩個array,這邊用calloc是因為他可以將動態記憶體配置的空間的值初始化為0,如果是用malloc的話,會不知道配置空間的初始值。
  • calloc(26, sizeof(int))26代表的是連續空間的大小,跟malloc一樣使用完要用free()釋放掉。

Code

Python

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        dict_s = {}
        dict_t = {}
        
        for index in range(len(s)):
            if s[index] in dict_s:
                dict_s[s[index]] += 1
            else:
                dict_s[s[index]] = 1
                
        for index in range(len(t)):    
            if t[index] in dict_t:
                dict_t[t[index]] += 1
            else:
                dict_t[t[index]] = 1
                
        return dict_s == dict_t

C

ver. 1
bool isAnagram(char * s, char * t){
    // int *dict_s = malloc(sizeof (int) * 26);
    int *dict_s = calloc(26, sizeof(int));
    // int *dict_t = malloc(sizeof (int) * 26);
    int *dict_t = calloc(26, sizeof(int));

    for (int index=0 ; index<strlen(s) ; index++){
        if (dict_s[(int)(s[index])-97] != 0){
            dict_s[(int)(s[index])-97]++;
        } else {
            dict_s[(int)(s[index])-97] = 1;
        }      
    }
        
    for (int index=0 ; index<strlen(t) ; index++){
        if (dict_t[(int)(t[index])-97] != 0){
            dict_t[(int)(t[index])-97]++;
        } else {
            dict_t[(int)(t[index])-97] = 1;
        } 
            
    }
    for (int index=0 ; index<26 ; index++){
        if (dict_s[index] != dict_t[index]){
            return false;
        }
    }
    
    return true;
}
ver. 2
bool isAnagram(char * s, char * t){
    // int *dict_s = malloc(sizeof (int) * 26);
    int *dict_s = calloc(26, sizeof(int));
    // int *dict_t = malloc(sizeof (int) * 26);
    int *dict_t = calloc(26, sizeof(int));
    
    for (int index=0 ; index<strlen(s) ; index++){
        dict_s[(int)(s[index])-97]++;     
    }
        
    for (int index=0 ; index<strlen(t) ; index++){
        dict_t[(int)(t[index])-97]++;
            
    }
    for (int index=0 ; index<26 ; index++){
        if (dict_s[index] != dict_t[index]){
            return false;
        }
    }
    
    return true;
}
ver. 3
bool isAnagram(char * s, char * t){
    // int *dict_s = malloc(sizeof (int) * 26);
    int *dict = calloc(26, sizeof(int));
    
    int index_s=0, index_t=0;
    while (index_s != strlen(s) || index_t != strlen(t)){
        if (index_s<strlen(s)){
            dict[(int)(s[index_s])-97]++;
            index_s++;
        }
        if (index_t<strlen(t)){
            dict[(int)(t[index_t])-97]--;
            index_t++;
        }
    }
    
    for (int index=0 ; index<26 ; index++){
        if (dict[index] != 0){
            return false;
        }
    }
    
    return true;
}

Result

  • Python

  • C

    • ver. 1

    • ver. 2

    • ver. 3

大家明天見/images/emoticon/emoticon29.gif


上一篇
Day 19 - Integer to Roman
下一篇
Day 21 - Robot Return to Origin
系列文
30天 Leetcode解題之路30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言